package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutil.CourseWebVoOrder;
import com.atguigu.commonutil.JwtUtils;
import com.atguigu.commonutil.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.feignclient.OrdersClient;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.vo.frontVo.CourseFrontVo;
import com.atguigu.eduservice.vo.frontVo.CourseWebVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }


    @PostMapping("/getConditionPage/{page}/{limit}")
    public R getConditionPage(@PathVariable Long page, @PathVariable Long limit,
                              @RequestBody CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        Map<String, Object> map = eduCourseService.getConditionPage(pageParam, courseFrontVo);

        return R.ok().data(map);
    }

    @GetMapping("/eduservice/courseFront/getCourseInfoOrder/{id}")
    public CourseWebVo getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo baseCourseInfo = eduCourseService.getBaseCourseInfo(id);
        return baseCourseInfo;
    }
}
